package cn.edu.klmy.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import web.models.CommonResult;
import web.models.Page;
import cn.edu.klmy.dao.ICourseStructureDao;
import cn.edu.klmy.models.course.CourseStructure;

@Controller
@RequestMapping("/courseStructure")
public class CourseStructureController {

	@Resource(name = "courseStructureDao")
	private ICourseStructureDao courseStructureDao;

	@RequestMapping("/add")
	@ResponseBody
	public CommonResult addCourseStructure(
			@ModelAttribute CourseStructure courseStructure) {
		courseStructureDao.addCourseStructure(courseStructure);
		return new CommonResult(new Boolean(true));
	}

	@RequestMapping("/update")
	@ResponseBody
	public CommonResult updateCourseStructure(
			@ModelAttribute CourseStructure courseStructure) {
		courseStructureDao.updateCourseStructure(courseStructure);
		return new CommonResult(new Boolean(true));
	}

	@RequestMapping("/remove")
	@ResponseBody
	public CommonResult removeCourseStructure(
			@RequestParam int courseStructureId) {
		courseStructureDao.removeCourseStructure("COURSE_STRUCTURE_ID", courseStructureId);
		return new CommonResult(new Boolean(true));
	}

	@RequestMapping("/exists")
	@ResponseBody
	public CommonResult courseStructureExists(@RequestParam String studyArea, @RequestParam String studySubject) {
        boolean result = courseStructureDao.courseStructureExists(studyArea, studySubject);
        return new CommonResult(result);
	}

	@RequestMapping("/{courseStructureId}")
	@ResponseBody
	public CourseStructure getCourseStructure(
			@PathVariable int courseStructureId) {
		return courseStructureDao.getCourseStructure(courseStructureId);
	}

	@RequestMapping("/all")
	@ResponseBody
	public Page<CourseStructure> getCourseStructuresPage(
			@RequestParam int page, @RequestParam int rows) {
		List<CourseStructure> courseStructures = courseStructureDao
				.getCourseStructures(null, page, rows);
		int count = courseStructureDao.getCourseStructureCount(null);
		return new Page<CourseStructure>(count, courseStructures);
	}

	@RequestMapping("/all-nopage")
	@ResponseBody
	public List<CourseStructure> getCourseStructures() {
		return courseStructureDao.getCourseStructures(null, 1, 10000);
	}

	public void setCourseStructureDao(ICourseStructureDao courseStructureDao) {
		this.courseStructureDao = courseStructureDao;
	}
}
